<?php

/**
 * @author gbfun <gbfun@163.com>  2017-09-16 20:03
 * http://www.wuweiyou.com
 */

class db_model_background_good extends db_model_good
{
    protected $_db_table;   
    protected $_formatter;
    
    protected static $_instance;
    
    public static function get_instance()
    {
        if (null === self::$_instance) {
            self::$_instance = new self();    //by gbfun 又是静态变量
        }
    
        return self::$_instance;
    }
    
    private function __clone()
    {
    }
    
    protected function __construct()
    {
    }

    public function get_db_table()
    {
        if (null === $this->_db_table) {
            $this->_db_table = db_table_background_good::get_instance();
        }
        
        return $this->_db_table;
    }
    
    public function add($good_info)
    {
        //var_dump($good_info); exit();
        $this->set_row_user($good_info);
        //var_dump($good_info); exit();
        
        $add_result = $this->get_db_table()->add($good_info);
        //var_dump($add_result);
        
        return $add_result;        
    }
    
    public function update($good_info)
    {
        $this->set_row_user($good_info);
        //var_dump($good_info); exit();
    
        $update_result = $this->get_db_table()->update($good_info);
        //var_dump($update_result);
    
        return $update_result;
    }
    
    public function add_good_attr_list($good_info, $good_attr_list)
    {        
        foreach ($good_attr_list as $good_attr) {
            $good_attr_id = $this->add_good_attr($good_info, $good_attr);                    
        }
    }
    
    public function add_good_attr($good_info, $good_attr)
    {    
        $good_attr_id = $this->get_db_table()->add_good_attr($good_info, $good_attr);
            
        return $good_attr_id;
    }

    
    public function add_good_sattr_list($good_info, $good_sattr_list)
    {   
        foreach ($good_sattr_list as $key => $good_sattr) {
            $good_sattr_id = $this->add_good_sattr($good_info, $good_sattr);
                        
            $good_sattr_list[$key]['id'] = $good_sattr_id;
        }
        
        return $good_sattr_list;
    } 
    
    public function add_good_sattr($good_info, $good_sattr)
    {    
        $add_good_sattr_result = $this->get_db_table()->add_good_sattr($good_info, $good_sattr);
    
        return $add_good_sattr_result;
    }
    
    public function add_good_number_grade_list($good_info, $good_number_grade_list)
    {    
        foreach ($good_number_grade_list as $key => $good_number_grade) {
            $good_number_grade_id = $this->add_good_number_grade($good_info, $good_number_grade);
    
            $good_number_grade_list[$key]['id'] = $good_number_grade_id;
        }
    
        return $good_number_grade_list;
    }
    
    public function add_good_number_grade($good_info, $good_number_grade)
    {    
        $add_good_number_grade_result = $this->get_db_table()->add_good_number_grade($good_info, $good_number_grade);
    
        return $add_good_number_grade_result;
    }
    
    public function add_good_spec_list($good_info, $good_spec_list, $good_sattr_list, $good_number_grade_list)
    {         
        $prices = array();  //商品全局价格
        foreach ($good_spec_list as $key => $good_spec) {            
            $good_spec = $this->add_good_spec($good_info, $good_spec);    
            
            $this->add_good_spec_sattr_list($good_info, $good_spec, $good_sattr_list);
            
            $good_spec = $this->add_good_spec_number_grade_list($good_info, $good_spec, $good_number_grade_list);
            
            //商品全局价格
            $prices = array_merge($prices, $good_spec['prices']);
        }
        
        //更新商品全局价格
        $good_info['min_price'] = min($prices);
        $good_info['max_price'] = max($prices);
        
        $update_good_price_stock_result = $this->get_db_table()->update_good_price($good_info);
    }
    
    public function add_good_spec($good_info, $good_spec)
    {        
        $good_spec_id = $this->get_db_table()->add_good_spec($good_info, $good_spec);
        $good_spec['id'] = $good_spec_id;
        //var_dump($good_spec_id);        
    
        return $good_spec;
    }
    
    public function add_good_spec_sattr_list($good_info, $good_spec, $good_sattr_list)
    {        
        $sattr_value_list = $good_spec['sattr_value_list'];
        //var_dump($sattr_value_list); exit();
        foreach ($sattr_value_list as $key => $sattr_value) {            
            $good_spec_sattr = array(
                'good_spec_id'  => $good_spec['id'], 
                //'good_sattr_id' => $good_sattr_id_map[$key],
                'good_sattr_id' => $good_sattr_list[$key]['id'],
                'value' => $sattr_value
            );
            //var_dump($good_spec_sattr); exit();
            
            $good_spec_sattr_id = $this->add_good_spec_sattr($good_info, $good_spec_sattr);
        }
    }
    
    public function add_good_spec_sattr($good_info, $good_spec_sattr)
    {    
        $add_good_spec_sattr_result = $this->get_db_table()->add_good_spec_sattr($good_info, $good_spec_sattr);
        //var_dump($add_good_spec_sattr_result);
    
        return $add_good_spec_sattr_result;
    }
    
    public function add_good_spec_number_grade_list($good_info, $good_spec, $good_number_grade_list)
    {    
        $number_grade_price_list = $good_spec['number_grade_price_list'];
        $good_spec_prices = array();  //商品全局价格
        foreach ($number_grade_price_list as $key => $number_grade_price) {           
            $good_spec_number_grade = array(
                'good_spec_id'  => $good_spec['id'],
                'good_number_grade_id' => $good_number_grade_list[$key]['id'],
                'price' => $number_grade_price,
                
            );
    
            $good_spec_number_grade = $this->add_good_spec_number_grade($good_info, $good_spec_number_grade);   
            
            $good_spec_prices[] = $good_spec_number_grade['price'];
        }
        
        $good_spec['prices'] = $good_spec_prices;
        
        return $good_spec;
    }
    
    public function add_good_spec_number_grade($good_info, $good_spec_number_grade)
    {    
        $good_spec_number_grade_id = $this->get_db_table()->add_good_spec_number_grade($good_info, $good_spec_number_grade);
        //var_dump($good_spec_number_grade_id);
        $good_spec_number_grade['id'] = $good_spec_number_grade_id;
    
        return $good_spec_number_grade;
    }
    
    /**
     * 删除相关【属性、规格、规格属性、价格范围】
     */
    public function remove_attr_spec_data($good_id)
    {    
        $remove_attr_spec_data_result = false;
        
        //删除【动态属性列表】
        $remove_attr_list_result = $this->get_db_table()->remove_attr_list($good_id);
        //var_dump($remove_attr_list_result);
        
        /***** @start 删除【规格】相关  *****/
        //获取【规格id列表】
        $spec_id_list = $this->get_db_table()->get_spec_id_list($good_id);
        //var_dump($spec_id_list); exit();
        
        //删除【规格属性【值】列表】
        $remove_spec_sattr_list_result = $this->get_db_table()->remove_spec_sattr_list($spec_id_list);
        //var_dump($remove_spec_sattr_list_result); exit();
        
        //删除【数量范围【价格】列表】
        $remove_spec_number_grade_list_result = $this->get_db_table()->remove_spec_number_grade_list($spec_id_list);
        //var_dump($remove_spec_number_grade_list_result); exit();
        
        //删除【规格列表】
        $remove_spec_list_result = $this->get_db_table()->remove_spec_list($spec_id_list);
        //var_dump($remove_spec_list_result); exit();
        
        //删除【规格属性列表】
        $remove_sattr_list_result = $this->get_db_table()->remove_sattr_list($good_id);
        //var_dump($remove_sattr_list_result); exit();
        
        //删除【数量范围列表】
        $remove_number_grade_list_result = $this->get_db_table()->remove_number_grade_list($good_id);
        //var_dump($remove_number_grade_list_result); exit();
        /***** @end 删除【规格】相关  *****/
        
        $remove_attr_spec_data_result = $remove_attr_list_result && $remove_spec_sattr_list_result && 
            $remove_spec_number_grade_list_result && $remove_spec_list_result && $remove_sattr_list_result && 
            $remove_number_grade_list_result;
        //var_dump($remove_attr_spec_data_result); exit();
    
        return $remove_attr_spec_data_result;
    }
}

